package com.hq.xmsb.actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.eredlab.g4.ccl.datastructure.Dto;
import org.eredlab.g4.ccl.datastructure.impl.BaseDto;
import org.eredlab.g4.ccl.json.JsonHelper;
import org.eredlab.g4.ccl.util.G4Constants;
import org.eredlab.g4.ccl.util.G4Utils;
import org.eredlab.g4.rif.report.excel.ExcelExporter;
import org.eredlab.g4.rif.report.fcf.FcfDataMapper;
import org.eredlab.g4.rif.report.fcf.GraphConfig;
import org.eredlab.g4.rif.report.fcf.Set;
import org.eredlab.g4.rif.web.BaseAction;
import org.eredlab.g4.rif.web.CommonActionForm;

import bsh.This;

import com.hq.xmsb.service.XmsbService;

public class XmsbAction extends BaseAction {
	//定义一个集合用于装载需要过滤的参数的名字因为是共用的，因此将其抽象出来定义为常量！
	private static final List<String> list=new ArrayList<String>();
	static
	{
		list.add("app_date");
		list.add("app_rp_date");
		list.add("site_app_date");
		list.add("map_design_unit");
		list.add("feas_rp_date");
		list.add("feas_at_date");
		list.add("lplan_cert_date");
		list.add("expr_at_date");
		list.add("expr_appr_date");
		list.add("remo_at_date");
		list.add("remo_appr_date");
		list.add("desi_date");
		list.add("prebid_bud_date");
		list.add("prebid_date");
		list.add("invi_begin_date");
		list.add("invi_date");
		list.add("cont_begin_date");
		list.add("cont_end_date");
		list.add("cont_sign_date");
	}
	private XmsbService xmsbService = (XmsbService) getService("xmsbService");
	//项目的显示页面
	public ActionForward xmsbIn(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		//移除属性名为QUERYCATALOGS4EXPORT_QUERYDTO的属性！每次初始化之前会移除！
		removeSessionAttribute(request, "QUERYCATALOGS4EXPORT_QUERYDTO");
		return mapping.findForward("xmsbjsp");
	}
	
	public ActionForward feiyongInit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		//移除属性名为QUERYCATALOGS4EXPORT_QUERYDTO的属性！每次初始化之前会移除！
		return mapping.findForward("feiyonginit");
	}
	/**
	 * 查询项目
	 * 
	 */
	public ActionForward querySfxm(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		//供下面的文件导出所用!
		super.setSessionAttribute(request, "QUERYCATALOGS4EXPORT_QUERYDTO", dto);
		List list = g4Reader.queryForPage("xmsbDemo.queryXmxx", dto);
		for(int i=0;i<list.size();i++)
		{
			Dto curr=(Dto)list.get(i);
			curr.put("areaname",(String)this.g4Reader.queryForObject("xmsbDemo.queryTpCdName",curr.getAsString("prj_area")));
			curr.put("classname",(String)this.g4Reader.queryForObject("xmsbDemo.queryTpCdName",curr.getAsString("prj_class")));
		}
		String jsonString = JsonHelper.encodeList2PageJson(list,  (Integer)this.g4Reader.queryForObject("xmsbDemo.queryXmxxCount"), G4Constants.FORMAT_Date);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}
	
	//根据状态来查询项目的信息有项目编号和项目名称！
	public ActionForward queryMessage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		List list = g4Reader.queryForPage("xmsbDemo.queryMessage", dto);
		String jsonString = JsonHelper.encodeList2PageJson(list,  (Integer)this.g4Reader.queryForObject("xmsbDemo.queryMessageCount"),null);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}
	/**
	 * 保存项目
	 */
	public synchronized ActionForward saveAllMessage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("提示：正在进入保存逻辑。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。");
		CommonActionForm aForm = (CommonActionForm) form;
		Dto inDto = aForm.getParamAsDto(request);
		System.out.println("-------------------------------------------------------------------");
		System.out.println(inDto+"共计"+(inDto.size()-2)+"个参数");
		System.out.println("-------------------------------------------------------------------");
		inDto=this.stringToDateFilter(list, inDto);
		this.xmsbService.saveSfxmDomain(inDto);
		this.setOkTipMsg("项目信息添加成功", response);
		return null;
	}
	/**
	 * 修改项目
	 */
	public synchronized ActionForward updateAllMessage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("提示：正在进入修改逻辑。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。");
		CommonActionForm aForm = (CommonActionForm) form;
		Dto inDto = aForm.getParamAsDto(request);
		System.out.println("-------------------------------------------------------------------");
		System.out.println("提示：要修改的项目的编号是"+inDto.getAsString("prj_sn"));
		System.out.println(inDto+"共计"+(inDto.size()-2)+"个参数");
		System.out.println("-------------------------------------------------------------------");
		//过滤所有为date类型的控件，将字符串转成成时间对象再处理！
		inDto=this.stringToDateFilter(list, inDto);
		this.xmsbService.updateSfxmDomain(inDto);
		setOkTipMsg("项目信息修改成功", response);
		return null;
	}
	
	//删除项目以及跟项目有关的十三张表
	public synchronized ActionForward deleteProject(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto inDto = aForm.getParamAsDto(request);
		String[] prjString=inDto.getAsString("strChecked").split(",");
		Dto delete=new BaseDto();
		for(int i=0;i<prjString.length;i++)
		{
			delete.put("prj_sn",prjString[i]);
			this.xmsbService.deleteSfxm(delete);
		}
		setOkTipMsg("多个项目删除成功", response);
		return mapping.findForward(null);
	}
	
	//编写一个方法专门用于将日期类型的参数进行处理,List集合用户装载需要过滤的时间控件的名字！
	private Dto stringToDateFilter(List<String> keys,Dto dto)
	{
		int cont=0;
		for(String str:keys)
		{
			if(dto.getAsString(str.trim().toLowerCase())!=null&&!dto.getAsString(str.trim().toLowerCase()).trim().equals(""))
			{
				dto.put(str,dto.getAsTimestamp(str.trim().toLowerCase()));
				cont++;
			}
		}
		System.out.println("提示：共过滤了"+cont+"个时间类型的参数。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。");
		return dto;
	}
	//通用的在查询出来是也在装换，是将时间转换成字符串
	private List dateToStringFilete(List<String> date,List<Object> list)
	{
		int cont=0;
		Dto dto=null;
		for(Object obj:list)
		{
			dto=(Dto)obj;
			for(String str:date)
			{
				dto.put(str,dto.getAsString(str));
			}
		}
		
		System.out.println("提示：共过滤了"+cont+"个时间类型的参数。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。");
		return list;
	}
	//将记录生成报表文件！
	public ActionForward exportExcel(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		//有意拖延2秒钟再执行！
		Thread.sleep(2000);
		Dto parametersDto = new BaseDto();
		parametersDto.put("reportTitle", "宜城市建投项目年度计划表");
		parametersDto.put("jbr", super.getSessionContainer(request).getUserInfo().getUsername());
		parametersDto.put("jbsj", G4Utils.getCurrentTime().toString());
		Dto inDto = (BaseDto) super.getSessionAttribute(request, "QUERYCATALOGS4EXPORT_QUERYDTO");
		inDto.put("rownum", "500");
		List fieldsList = null;
		fieldsList = g4Reader.queryForList("xmsbDemo.queryXmxxForPrint", inDto);
		parametersDto.put("countXmid", new Integer(fieldsList.size()));// 合计条数
		ExcelExporter excelExporter = new ExcelExporter();
		excelExporter.setTemplatePath("/xmsb/xmsbYearReport.xls");
		excelExporter.setData(parametersDto, fieldsList);
		excelExporter.setFilename("宜城市建投项目年度计划表.xls");
		excelExporter.export(request, response);
		return mapping.findForward(null);
	}

	//查询统计数据并格式化成xml用于显示图表
	public ActionForward queryFeiyongXml(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		System.out.println("提示：正在生成项目编号为"+dto.getAsString("prj_sn")+"的图表。。。。");
		String prj_sn=dto.getAsString("prj_sn");
		//List list = g4Reader.queryForList("report.niandu", prj_sn);
		//实例化一个图形配置对象
		GraphConfig graphConfig = new GraphConfig();
		//主标题
		graphConfig.setCaption("项目费用一览（单位：元）");
		//X坐标轴名称
		graphConfig.setXAxisName("费用类型");
		//数字值前缀
		//graphConfig.setNumberPrefix("$");
		List dataList = new ArrayList();
		/*
		 * 这里我们要做个小处理，由于并不是每个费用类型下都一定
		 * 有金额，但是我们在页面显示每种费用类型下的金额，及时金额为0
		 * 所以我们在组装图形时要用类型的个数来循环，那么首先我们要先查询
		 * 出所有费用类型的信息
		 */
	
			//构造没有查询到的费用类型，也要为其构造图形！
			Set set1 = new Set();	
			set1.setName("预算金额"); //名称
			String ysje=(String)g4Reader.queryForObject("xmsbDemo.queryYsjeByPrj_sn",prj_sn);
			set1.setValue(ysje==null?"0":ysje); //数据值
			dataList.add(set1);
			Set set2 = new Set();	
			set2.setName("合同金额"); //名称
			String htje=(String)g4Reader.queryForObject("xmsbDemo.queryHtjeByPrj_sn",prj_sn);
			set2.setValue(htje==null?"0":htje); //数据值
			dataList.add(set2);
			Set set3 = new Set();	
			set3.setName("已付金额"); //名称
			String yfje=(String)g4Reader.queryForObject("xmsbDemo.queryYfjeByPrj_sn",prj_sn);
			set3.setValue(yfje==null?"0":yfje); //数据值
			dataList.add(set3);
		
		//将图表数据转为Flash能解析的XML资料格式
		String xmlString = FcfDataMapper.toFcfXmlData(dataList, graphConfig);
		Dto outDto = new BaseDto();
		outDto.put("success", new Boolean(true));
		outDto.put("xmlstring", xmlString);
		write(JsonHelper.encodeObject2Json(outDto), response);
		return mapping.findForward(null);
	}
	//获得区域编号和名称
	public  ActionForward queryPrjAreas(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		System.out.println("提示：正在加载区域类型。。。。"+dto);
		List list=this.g4Reader.queryForList("xmsbDemo.queryPrjAreas");
		System.out.println("========================="+dto.getAsString("type"));
		if(dto.getAsString("type").trim().equals("all"))
		{
			//添加一个全部状态的选项
			Dto empty=new BaseDto();
			empty.put("tp_cd","");
			empty.put("tp_name", "全部");
			list.add(empty);
		}
		this.write(JsonHelper.encodeList2PageJson(list, list.size(),null), response);
		return null;
	}
	//获得区域编号和名称
	public  ActionForward queryPrjClasses(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		System.out.println("提示：正在加载项目。。。。"+dto);
		List list=this.g4Reader.queryForList("xmsbDemo.queryPrjClasses");
		if(dto.getAsString("type").trim().equals("all"))
		{
			//添加一个全部状态的选项
			Dto empty=new BaseDto();
			empty.put("tp_cd","");
			empty.put("tp_name", "全部");
			list.add(empty);
		}
		this.write(JsonHelper.encodeList2PageJson(list, list.size(),null), response);
		return null;
	}
	//检查用户填写的项目编号是否存在！
	public  ActionForward existPrjNo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		System.out.println("提示：正在验证是否存在。。。。。。。。。。。。。。");
		Integer cont=(Integer)this.g4Reader.queryForObject("xmsbDemo.existPrjNo",dto.getAsString("prj_no"));
		if(cont==null||cont==0)
		{
			this.setOkTipMsg("false", response);
		}
		else
		{
			this.setErrTipMsg("true", response);
		}
		return null;
	}
}
